from time import sleep

from numpy.f2py.cfuncs import needs

from common_util import save_recipes_pretty
from dai_calc import compare_needs_arr, get_needs_name_arr
from parse_html_data_list import read_from_file
from parse_html_data_list import get_a_tag_titles
from get_html_merge_info2 import fetch_nested_formula_elements
from parse_receipts2 import parse_receipts2, parse_origin

base_url = "https://wiki.biligame.com"


def do_extra_deal(total_res, remove_dict = {
        "氢" : [["精炼油", "氢"], ["原油"]],
        "精炼油" : [["原油"], ["精炼油", "氢","煤矿"]]
    }):
    real_res = []
    for elem in total_res:
        rm_needs = remove_dict.get(elem["name"], None)
        if rm_needs is None:
            real_res.append(elem)
            continue
        keep = True
        for rm_need in rm_needs:
            if compare_needs_arr(rm_need, get_needs_name_arr(elem["needs"])):
                keep = False
                break
        if keep:
            real_res.append(elem)
    real_res.append(  {
    "name": "氢",
    "needs": [
      {
        "name": "原油",
        "mount": 2
      }
    ],
    "time": 8,
    "mount": 3,
    "machine": "精炼"
  })
    real_res.append(  {
    "name": "精炼油",
    "needs": [
      {
        "name": "原油",
        "mount": 2
      },
      {
        "name": "煤矿",
        "mount": 1
      }
    ],
    "time": 8,
    "mount": 3,
    "machine": "精炼"
  })
    return real_res

def download_parse_one(elem):
    url = f'{base_url}{elem["href"]}'
    formula_elements = fetch_nested_formula_elements(
        url,
        parent_class="item",
        target_class="formula",
        target2_class="item-info"
    )
    if not formula_elements:
        msg = f"未找到配方：【{elem}】: {url}"
        print(msg)
        # raise msg
        return None
    res = []
    for elements in formula_elements:
        json_res = None
        if elements["type"] == "merge":
            json_res = parse_receipts2(elements["html"])
            if json_res:
                res.extend(json_res)
        else:
            gather_device = parse_origin(elements["html"])
            if gather_device is None:
                print(f"{elem['title']} 无采集设备，跳过.")
            else:
                json_res = {
                    "name": elem['title'],
                    "needs": [],
                    "time": -1,
                    "mount": 1,
                    "machine": gather_device
                }
            if json_res:
                res.append(json_res)
    return res

def download_receipts(receipt_path):
    list_data_txt = read_from_file('C:/workspaceFinal/pycharm/dai_seb_qiu_calc/data/list_data.txt')
    goods_list = get_a_tag_titles(list_data_txt)

    idx = 0
    total_res = []
    for elem in goods_list:
        # if not (elem["title"] == "氢"):
        # if not (elem["title"] == "氢" or elem["title"] == "精炼油"):
        #     continue
        json_res = None
        req_time = 0
        while(json_res is None and req_time < 5):
            json_res = download_parse_one(elem)
            req_time += 1
            if json_res is None:
                print(f"【{elem['title']}】 第{req_time}次请求失败，继续重试.")
                sleep(5)

        if not json_res:
            print(f"【{elem['title']}】 5次请求均失败，跳过.")
            continue
        idx += 1
        total_res.extend(json_res)
        save_recipes_pretty(json_res, f"{receipt_path}{idx}_{elem['title']}.json")
    deal_res = do_extra_deal(total_res)
    save_recipes_pretty(deal_res, f"{receipt_path}0_全集结果.json")


if __name__ == '__main__':
    download_receipts("C:/workspaceFinal/pycharm/dai_seb_qiu_calc/data/receipts_html/")
